ECE3730J RC3_LAB4
For this RC, we will go through Lab 4 in detail.
CubeMX
We need to generate PWM signal on PB14
(embeded with an LED D1
, or D2
if you like)
In STM32, Input Capture and Output Compare is configured in Timer channels.
Let’s check which Timer is connected to PB14
Then we configure timer 1.
Prescaler: 7200-1 Counter Period: 200-1 (50Hz) Pulse: 180 (duty cycle of 90%; TIM_CNT 0~179: HIGH, TIM_CNT 180~199: LOW) CHN Polarity: LOW (needed for CHN) > CHxN is the complementary channel of CHx in Timer1. > If the polarity of CHxN and CHx are different, the two channels will follow the same output pattern, otherwise they will be complementaryAlso remember to configure PB12
(SW4
) as GPIO_Input.
1 | /* USER CODE BEGIN 2 */ |
Change PWM duty cycle:
1 | void ChangeDutyCycle(int pulse_width) |
Get the press/release action of the button (PB12
):
1 | int pressed = 0; |